package gui;

import java.awt.Component;
import java.awt.Dimension;
import java.text.DecimalFormat;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import common.Constants;
import common.EarthSimSettings;

public class PhysicalControlPanel extends JPanel {
  private static final long serialVersionUID = -6227615824913446037L;

  private EarthSimSettings settings = EarthSimSettings.getInstance();
  private JSpinner tilt;
  private JSpinner eccentricity;
  
  public PhysicalControlPanel() {
    super();
    setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
    setBorder(BorderFactory.createTitledBorder("Physical Settings"));
    setAlignmentX(Component.LEFT_ALIGNMENT);
    
    Dimension dim = new Dimension(190,90);
    setPreferredSize(dim);
    setMaximumSize(dim);
    
    add(getTiltControl());
    add(Box.createVerticalStrut(10));
    add(getEccentricityControl());
  }

  private JPanel getTiltControl() {
    tilt = new JSpinner(
      new SpinnerNumberModel(Constants.DEFAULT_TILT, 
                             Constants.MIN_TILT, 
                             Constants.MAX_TILT, 1));
    tilt.setPreferredSize(new Dimension(75, 20));
    tilt.setMaximumSize(new Dimension(75, 20));
    tilt.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent e) {
        int tiltOfEarth = Integer.parseInt(tilt.getValue().toString());
        settings.setTilt(tiltOfEarth);        
      };
    });
    
    JPanel pnl = new JPanel();
    pnl.setLayout(new BoxLayout(pnl, BoxLayout.LINE_AXIS));
    pnl.setAlignmentX(Component.LEFT_ALIGNMENT);
    pnl.add(new JLabel("Axial tilt: "));
    pnl.add(tilt);
    return pnl;
  }

  private JPanel getEccentricityControl() {
    eccentricity = new JSpinner(
      new SpinnerNumberModel(Constants.DEFAULT_ECCENTRICITY, 
                             Constants.MIN_ECCENTRICITY, 
                             Constants.MAX_ECCENTRICITY, .01));
    
    JSpinner.NumberEditor editor = (JSpinner.NumberEditor)eccentricity.getEditor();
    DecimalFormat format = editor.getFormat();
    format.setMinimumFractionDigits(2);
    format.setMaximumFractionDigits(2);
    format.applyPattern("#.##");
    editor.getTextField().setHorizontalAlignment(SwingConstants.RIGHT);
    
    eccentricity.setPreferredSize(new Dimension(75, 20));
    eccentricity.setMaximumSize(new Dimension(75, 20));
    eccentricity.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent e) {
        float ecc = Float.parseFloat(eccentricity.getValue().toString());
        settings.setEccentricity(ecc);        
      };
    });
    
    JPanel pnl = new JPanel();
    pnl.setLayout(new BoxLayout(pnl, BoxLayout.LINE_AXIS));
    pnl.setAlignmentX(Component.LEFT_ALIGNMENT);
    pnl.add(new JLabel("Orbital Eccentricity: "));
    pnl.add(eccentricity);
    return pnl;
  }
  
  public void disable() {
    tilt.setEnabled(false);
    eccentricity.setEnabled(false);
  }
  
  public void enable() {
    tilt.setEnabled(true);
    eccentricity.setEnabled(true);
  }
}
